﻿using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class CanvasEditor : MonoBehaviour {
	// === Unity ======================================================================================================
	private void Awake() {
		_managerTexture = Managers.Texture;

		_menuBar = new UIMenuBar();
		var menuItemNew = new UIMenuItem(new ButtonNew(), "NEW");
		menuItemNew.SetEnabled(false);
		var menuItemSave = new UIMenuItem(new ButtonSave(), "SAVE");
		var menuItemLoad = new UIMenuItem(new ButtonLoad(), "LOAD");
		var menuItemClose = new UIMenuItem(new ButtonClose(), "CLOSE");
		menuItemClose.SetEnabled(false);
		_menuBar.Add(menuItemNew, menuItemSave, menuItemLoad, menuItemClose);

		_listViewModel = new UIListView(new Vector2(0, _menuBar.GetSize().y));
		var listViewElementPos = new Vector2(_listViewModel.GetLocation().x + _listViewModel.GetSize().x, _listViewModel.GetLocation().y);

		_listViewElementOfTile = new UIListView(listViewElementPos, 3);
		_listViewElementOfTile.Add(GetButtonsOfTextures(_managerTexture.GetTextures(ElementType.Tile)));
		_listViewElementOfWall = new UIListView(listViewElementPos, 3);
		_listViewElementOfWall.Add(GetButtonsOfTextures(_managerTexture.GetTextures(ElementType.Wall)));
		_listViewElementOfEraser = new UIListView(listViewElementPos, 3);
		_listViewElementOfEraser.Add(GetButtonsOfTextures(_managerTexture.GetTextures(ElementType.Eraser)));
		
		var listItemModelTile = new UIListItem(new ButtonModel(_listViewModel, _listViewElementOfTile, ElementType.Tile),
			_managerTexture.GetFirstTexture(ElementType.Tile), "Tile");
		var listItemModelWall = new UIListItem(new ButtonModel(_listViewModel, _listViewElementOfWall, ElementType.Wall),
			_managerTexture.GetFirstTexture(ElementType.Wall), "Wall");
		var listItemModelEraser = new UIListItem(new ButtonModel(_listViewModel, _listViewElementOfEraser, ElementType.Eraser),
			_managerTexture.GetFirstTexture(ElementType.Eraser), "Eraser");
		_listViewModel.Add(listItemModelTile, listItemModelWall, listItemModelEraser);

		_listViewModel.GetItems().First().GetButtonModel().Action();
	}

	private void Start() {
		var _camera = MapEditorBaseData.GetInstance.Camera.camera;
		if (_camera == null) {
			return;
		}
		var viewPort = _camera.ScreenToViewportPoint(new Vector3(380, _menuBar.GetSize().y, 0));
		_camera.rect = new Rect(viewPort.x, 0, 1 - viewPort.x, 1 - viewPort.y);
	}

	private void OnGUI() {
		ShowPanels();
	}

	private UIListItem[] GetButtonsOfTextures(IEnumerable<Texture> textures) {
		return textures.Select(texture => new UIListItem(new ButtonElement(texture), texture, texture.name)).ToArray();
	}

	// === Private ====================================================================================================
	private ManagerTexture _managerTexture;

	private UIMenuBar _menuBar;
	private UIListView _listViewModel;
	private UIListView _listViewElementOfTile;
	private UIListView _listViewElementOfWall;
	private UIListView _listViewElementOfEraser;

	// === Show ===
	private void ShowPanels() {
		ShowMenuBar();
		ShowListView(_listViewModel);
		ShowListView(_listViewElementOfTile);
		ShowListView(_listViewElementOfWall);
		ShowListView(_listViewElementOfEraser);
	}

	private void ShowMenuBar() {
		GUI.Box(_menuBar.GetRect(), _menuBar.GetText(), _menuBar.GetStyle());
		foreach (var uiMenuItem in _menuBar.GetItems()) {
			if (GUI.Button(uiMenuItem.GetRect(), uiMenuItem.GetText(), uiMenuItem.GetStyle())) {
				if (uiMenuItem.IsEnabled()) {
					uiMenuItem.DoAction();
				}
			}
		}
	}

	private void ShowListView(UIListView listView) {
		if (!listView.IsEnabled()) {
			return;
		}
		GUI.Box(listView.GetRect(), listView.GetText(), listView.GetStyle());
		foreach (var uiListItem in listView.GetItems()) {
			if (GUI.Button(uiListItem.GetRect(), uiListItem.GetText(), uiListItem.GetStyle())) {
				if (uiListItem.IsEnabled()) {
					uiListItem.DoAction();
				}
			}
			var texture = uiListItem.GetUITexture();
			var label = uiListItem.GetUILabel();
			GUI.DrawTexture(texture.GetRect(), texture.GetTexture(), texture.GetScaleMode());
			GUI.Label(label.GetRect(), label.GetText(), label.GetStyle());
		}
	}
}
